package com.pzhu.service.impl;

import com.pzhu.mapper.BrandeMapper;
import com.pzhu.pojo.Brand;
import com.pzhu.pojo.PageBean;
import com.pzhu.service.BrandService;
import com.pzhu.util.SqlSessionFactoryUtil;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class BrandServiceImpl implements BrandService {
    public List<Brand> selectAll() {
          //自定义一个SqlSessionFactoryUtil类来通过静态方法获取sqlSessionFactory对象
           SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
        //2.获取sqlSession对象
            SqlSession sqlSession = sqlSessionFactory.openSession();
            //3.获取代理对象
            BrandeMapper brandeMapper = sqlSession.getMapper(BrandeMapper.class);

            //4.调用查询所有的方法
            List<Brand> brands = brandeMapper.selectAll();
            //释放资源
            sqlSession.close();

            return brands;

    }

    /**
     * 添加一条记录
     * @param brand
     */
    public void add(Brand brand){
        //自定义一个SqlSessionFactoryUtil类来通过静态方法获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
        //2.获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.获取代理对象
        BrandeMapper brandeMapper = sqlSession.getMapper(BrandeMapper.class);
        //4.调用查询所有的方法
        brandeMapper.add(brand);
//        5提交事物
        sqlSession.commit();
        //释放资源
        sqlSession.close();

    }

    public void deletByIds(int[] ids) {
        //自定义一个SqlSessionFactoryUtil类来通过静态方法获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
        //2.获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.获取代理对象
        BrandeMapper brandeMapper = sqlSession.getMapper(BrandeMapper.class);
        //4.调用批量删除方法做删除
         brandeMapper.deleteByIds(ids);
//        5提交事物
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

    public PageBean<Brand> selectByPage(int begin, int size) {
        //自定义一个SqlSessionFactoryUtil类来通过静态方法获取sqlSessionFactory对象
        SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtil.getSqlSessionFactory();
        //2.获取sqlSession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3.获取代理对象
        BrandeMapper brandeMapper = sqlSession.getMapper(BrandeMapper.class);
//分页查询到的数据结果集
        List<Brand> brands = brandeMapper.selectByPage(begin, size);
//查询数据库表中总记录数
        int totalCount = brandeMapper.selectTotalCount();

//        将分页查询的结果集及总记录数封装到PageBean中
        PageBean<Brand> pageBean = new PageBean<Brand>();
        pageBean.setRows(brands);
        pageBean.setTotalCount(totalCount);

        return pageBean;
    }
}
